﻿using Niunan.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Niunan.VideoSys.Web.Areas.Adnn1n.Controllers
{
    public class HomeController : Controller
    {
        // GET: Adnn1n/Home
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Top()
        {

            return View();
        }
        public ActionResult Left()
        {
            return View();
        }

        //HTML5上传
        public ActionResult H5Upload() { 
            HttpPostedFileBase file = Request.Files["fileToUpload"];
            String fileName = file.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();

            //定义允许上传的文件扩展名  
            Hashtable extTable = new Hashtable();
            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable["image"]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            { 
                return Json(new
                {
                    status = "no",
                    info = "上传文件扩展名是不允许的扩展名。  只允许" + ((String)extTable["image"]) + "格式。"
                });
            }
            string dirpath = "/upload/" + DateTime.Now.ToString("yyyyMMdd") + "/";
            if (!Directory.Exists(Server.MapPath(dirpath)))
            {
                Directory.CreateDirectory(Server.MapPath(dirpath));
            }
            String newFileName = Guid.NewGuid().ToString().Substring(0,6) + fileExt;

            file.SaveAs(Server.MapPath(dirpath) + newFileName);

            //改为200*100
            Tool.CreateImage(Server.MapPath(dirpath) + newFileName, Server.MapPath(dirpath) + newFileName+ "_200x100.jpg", 200, 100);
            //删除原图
            try
            {
                System.IO.File.Delete(Server.MapPath(dirpath) + newFileName);
            } 
            catch (Exception)
            { 
            }

            return Json(new { status = "ok", path = dirpath + newFileName + "_200x100.jpg" });
        }

        //HTML5上传 - mp4
        public ActionResult H5Upload_MP4()
        {
            HttpPostedFileBase file = Request.Files["fileToUpload"];
            String fileName = file.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();

            //定义允许上传的文件扩展名  
            Hashtable extTable = new Hashtable();
            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("mp4", "mp4");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable["mp4"]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                return Json(new
                {
                    status = "no",
                    info = "上传文件扩展名是不允许的扩展名。  只允许" + ((String)extTable["mp4"]) + "格式。"
                });
            }
            string dirpath = "/upload/" + DateTime.Now.ToString("yyyyMMdd") + "/";
            if (!Directory.Exists(Server.MapPath(dirpath)))
            {
                Directory.CreateDirectory(Server.MapPath(dirpath));
            }
            String newFileName = Guid.NewGuid().ToString().Substring(0, 6) + fileExt;

            file.SaveAs(Server.MapPath(dirpath) + newFileName);
             

            return Json(new { status = "ok", path = dirpath + newFileName   });
        }

        /// <summary>
        /// layui编辑器里的上传图片功能 
        //{
        //  "code": 0 //0表示成功，其它失败
        //  ,"msg": "" //提示信息 //一般上传失败后返回
        //  ,"data": {
        //    "src": "图片路径"
        //    ,"title": "图片名称" //可选
        //  }
        //}
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ImgUpload()
        {
            var imgFile = Request.Files[0];
            if (imgFile != null && !string.IsNullOrEmpty(imgFile.FileName))
            {

                string tempname = "";
                var filename = imgFile.FileName;
                var extname = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf(".")); //扩展名，如.jpg

                #region 判断后缀
                if (!extname.ToLower().Contains("jpg") && !extname.ToLower().Contains("swf") && !extname.ToLower().Contains("gif") && !extname.ToLower().Contains("mp4"))
                {
                    return Json(new { code = 1, msg = "只允许上传jpg,mp4,swf,gif格式的文件.", });
                }
                #endregion

                #region 判断大小
                long mb = imgFile.ContentLength / 1024 / 1024; // MB
                if (mb > 5)
                {
                    return Json(new { code = 1, msg = "只允许上传小于 5MB 的文件.", });
                }
                #endregion

                var filename1 = System.Guid.NewGuid().ToString().Substring(0, 6) + extname;
                tempname = filename1;
                var path = "/upload/";
                string dir = DateTime.Now.ToString("yyyyMMdd");
                //完整物理路径
                string wuli_path = Server.MapPath($"{path}/{dir}/");
                if (!System.IO.Directory.Exists(wuli_path))
                {
                    System.IO.Directory.CreateDirectory(wuli_path);
                }
                filename = wuli_path + filename1;

                imgFile.SaveAs(filename);



                return Json(new { code = 0, msg = "上传成功", data = new { src = $"/upload/{dir}/{filename1}", title = filename1 } });
            }
            return Json(new { code = 1, msg = "上传失败", });
        }


    }
}